package me.zhengjie.modules.web.rest.advert;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.config.entity.ResponseBaseEntity;
import me.zhengjie.config.enums.AtmrErrorEnum;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.web.domain.advert.AdvertReq;
import me.zhengjie.modules.web.domain.advert.AdvertResp;
import me.zhengjie.modules.web.service.advert.IAdvertWebService;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "前端接口-广告")
@RequestMapping("web/advert")
public class AdvertWebController {

    @Autowired
    private IAdvertWebService advertService;
    
    @PostMapping("getAdvertListById")
    @ApiOperation("前端接口-根据广告位获取广告列表")
    public ResponseBaseEntity getAdvertListById(@RequestBody AdvertReq req){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (StringUtils.isBlank(req.getType())){
                throw new WeChatException("缺少参数");
            }
            List<AdvertResp> list = advertService.advertList(req.getType());
            response.setBody(list);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
}
